package com.evan.common.registry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Description: 基础注册器抽象类
 * @Author liudeguang
 */
public abstract class BaseRegistry<K, V> implements Registry<K, V> {

    protected Map<K, V> map = new ConcurrentHashMap();

    @Override
    public V register(K k, Function<? super K, ? extends V> mappingFunction) {
        return map.computeIfAbsent(k, mappingFunction);
    }

    @Override
    public V register(K k, V v) {
        return map.put(k, v);
    }

    @Override
    public V get(K k) {
        return map.get(k);
    }

    @Override
    public void forEach(BiConsumer<? super K, ? super V> action) {
        map.forEach(action);
    }

    @Override
    public void forEach(Consumer<? super V> action) {
        forEach((k, v) -> action.accept(v));
    }
}
